package com.xindaoapp.happypet.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout.LayoutParams;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.RoundImageView2;

public class NotePictureAdapter extends BaseAdapter {
    private ArrayList<String> picUrList = new ArrayList<String>();
    private final Context context;
    DisplayImageOptions defaultOptions;

    public NotePictureAdapter(Context context, ArrayList<String> picUrList) {
        this.context = context;
        this.picUrList = picUrList;
        defaultOptions =
                new DisplayImageOptions.Builder()
                        .resetViewBeforeLoading(true)
                        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                        // .displayer(new FadeInBitmapDisplayer(1000))
                        .bitmapConfig(Bitmap.Config.RGB_565)
                        .showImageForEmptyUri(R.drawable.picture_loading)
                        .showImageOnFail(R.drawable.picture_loading)
                        .showStubImage(R.drawable.picture_loading).cacheInMemory(true)
                        .cacheOnDisc(false).build();
    }

    @Override
    public int getCount() {
        if (picUrList.size() >= 0 && picUrList.size() < Constants.PUBLISHPOSTS_COUNTS) {
            return picUrList.size() + 1;
        } else {
            return picUrList.size();
        }
    }

    public void resetList(ArrayList<String> picUrList) {
        this.picUrList = picUrList;
    }

    public ArrayList<String> getDatas() {
        return picUrList;
    }

    @Override
    public Object getItem(int arg0) {
        return picUrList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View contentView, ViewGroup arg2) {
        contentView =
                LayoutInflater.from(context).inflate(R.layout.note_picture_gridview_item, null);
        RoundImageView2 imageView = (RoundImageView2) contentView.findViewById(R.id.picture);

        LayoutParams params =
                new LayoutParams(CommonUtil.dip2px(context, 180) / 2, CommonUtil.dip2px(context,
                        180) / 2);
        imageView.setLayoutParams(params);

        if (Constants.PUBLISHPOSTS_COUNTS != picUrList.size() && position == picUrList.size()) {
            // imageView.setScaleType(ScaleType.CENTER_INSIDE);
            imageView.setImageDrawable(null);
            imageView.setBackgroundResource(R.drawable.image_normal);
        } else {
            // imageView.setScaleType(ScaleType.CENTER);
            ImageLoader.getInstance().displayImage("file://" + picUrList.get(position), imageView,
                    defaultOptions);
            imageView.setBackgroundColor(context.getResources().getColor(R.color.white));
            imageView.setNeedCiry(true);
        }
        return contentView;
    }

    static class ViewHolder {
        RoundImageView2 imageView;
    }

}
